/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.view;

import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.WebConfigManager;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.config.ActionConfigBean;
import com.jspx.txweb.config.TXWebConfigManager;
import com.jspx.txweb.dao.PermissionDAO;
import com.jspx.txweb.env.TXWebIoc;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.table.Member;
import com.jspx.txweb.table.MemberRole;
import com.jspx.txweb.table.OperateVO;
import com.jspx.txweb.table.Role;
import com.jspx.utils.StringUtil;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2010-11-8
 * Time: 16:26:27
 * 权限查看
 */
@HttpMethod(caption = "权限")
public class PermissionView extends ActionSupport {
    private WebConfigManager webConfigManager = TXWebConfigManager.getInstance();

    public PermissionView() {

    }

    protected PermissionDAO permissionDAO;
    @Ref(name = TXWebIoc.permissionDAO)
    public void setPermissionDAO(PermissionDAO permissionDAO) {
        this.permissionDAO = permissionDAO;
    }
    private String find = StringUtil.empty;
    public void setFind(String find) {
        this.find = find;
    }
    public String getFind() {
        return find;
    }
    /**
     * @return 命名空间列表
     * @throws Exception
     */
    public List<String> getNamespaceList() throws Exception {
        return webConfigManager.getNamespaceList();
    }

    /**
     * @return 继承关系表
     * @throws Exception
     */
    public Map<String, String> getExtendList() throws Exception {
        return webConfigManager.getExtendList();
    }

    public Map<String, ActionConfigBean> getActionMap() throws Exception {
        return webConfigManager.getActionMap(permissionDAO.getNamespace());
    }


    private int id = 0;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    /**
     * @param id 用户ID
     * @return 得到用户角色
     * @throws SQLException
     */
    public Role getMemberRole(long id) throws SQLException {
        MemberRole memberRole = permissionDAO.getMemberRole(id);
        if (memberRole != null) return memberRole.getRole();
        return new Role();
    }

    /**
     * @return 操作列表
     * @throws Exception 异常
     */
    public Collection<OperateVO> getOperateList() throws Exception {
        return webConfigManager.getOperateList(permissionDAO.getNamespace());
    }

    /**
     * @return 角色列表
     * @throws SQLException 查询异常
     */
    public List<Role> getList() throws SQLException {
        return permissionDAO.getRoleList(find);
    }

    /**
     * @return 得到选项
     * @throws SQLException
     */
    public String getOptionList() throws SQLException {
        StringBuilder sb = new StringBuilder();
        List<Role> roles = getList();
        sb.append(0).append(":").append("无;");
        for (int i = 0; i < roles.size(); i++) {
            Role role = roles.get(i);
            sb.append(role.getId()).append(":").append(role.getName());
            if (i < roles.size() - 1) sb.append(StringUtil.SEMICOLON);
        }
        return sb.toString();
    }

    public int getTotalCount() throws Exception {
        return permissionDAO.getRoleCount(find);
    }

    public String execute() throws Exception {
        // public IRole getRole() 方法和默认冲突
        put("role", permissionDAO.load(Role.class,id));
        put("member", permissionDAO.load(Member.class, getLong("uid")));
        return getActionResult();
    }

}